﻿package 
{
	import be.wellconsidered.services.webservice.types.WebServiceComplexType;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	
	import PaperBase;
	import Space.View.MapView;
	import GameData;
	import GameEvents;
	
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.objects.parsers.Collada;
	import org.papervision3d.objects.DisplayObject3D;

	import org.osflash.thunderbolt.Logger;
	import Space.Interface.ResourcesView;
	import Space.SkyMap;
	import Space.Interface.ObjectInformation;
	
	import Utils.WebServiceCommunication;
	
	public class Space extends PaperBase {
		
		private var CameraMove:Boolean = false;
		private var cameraMoveX:Number = 1;
		private var cameraMoveY:Number = 1;
		private var mousePositionX:Number;
		private var mousePositionY:Number;
		private var cameraObject:DisplayObject3D;
		private var data:GameData;
		private var objInf:ObjectInformation;
		
		public function Space():void {
			data = new GameData();
			init();
		}
		
		override protected function init3d():void{
			
			MapViewBuild();
			var background:SkyMap = new SkyMap();
			default_scene.addChild(background);
			var rv:Space.Interface.ResourcesView = new Space.Interface.ResourcesView(data.ResourcesData);
			rv.x = stage.width/2 - rv.width/2+5;
			addChild(rv);
			default_camera.x = 0
			default_camera.y = 0;
			default_camera.z = 2000;
			default_camera.zoom  = 100;
			default_camera.lookAt(cameraObject);
			default_camera.orbit(-90, 0, true, cameraObject);
			cameraMoveY = -90;
			cameraMoveX = 0;
		}
		
		public function MapViewBuild():void 
		{
			var mapView:Space.View.MapView = new Space.View.MapView(data.FleetObject);
			default_scene = mapView.Map;
			mapView.Map.addEventListener("Object_Over", MapObjOver);
			mapView.Map.addEventListener("Object_Out", MapObjOut);
			cameraObject = mapView.cameraObject;
		}
		
		private function MapObjOut(e:GameEvents):void 
		{
			removeChild(objInf);
		}
		
		override protected function processFrame():void{
			if (CameraMove)
			{
				cameraMoveX += stage.mouseX - mousePositionX;;
				cameraMoveY += stage.mouseY - mousePositionY;
				if (cameraMoveY	 > -1) cameraMoveY = -1;
				if (cameraMoveY	 < -180) cameraMoveY = -180;
				default_camera.orbit(cameraMoveY, -cameraMoveX, true, cameraObject);
				mousePositionX = stage.mouseX;
				mousePositionY = stage.mouseY

			}
		}
		
		override protected function initEvents():void{
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, MoveCamera);
			stage.addEventListener(MouseEvent.MOUSE_UP, StopCamera);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, ZoomCamera);
		}
		
		private function MapObjOver(e:GameEvents):void 
		{
			objInf = new Space.Interface.ObjectInformation(e.command.toString(), this.mouseX, this.mouseY);
			addChild(objInf);
		}

		private function MoveCamera(ThisEvent:MouseEvent):void{
			CameraMove = true;
			mousePositionX = ThisEvent.stageX;
			mousePositionY = ThisEvent.stageY;
		}
		
		private function StopCamera(ThisEvent:MouseEvent):void{
			CameraMove = false;
		}
		
		private function ZoomCamera(ThisEvent:MouseEvent):void{
			if (ThisEvent.delta > 0){
				default_camera.moveForward(ThisEvent.delta * 10);
			}else {
				default_camera.moveBackward(-ThisEvent.delta * 10);
			}
		}
		
	}
	
}